package com.inossem.warehousestorage_inossem.adapter.changeStorage;

import android.content.Context;
import android.content.Intent;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.utils.log.LogUtils;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.change_storage.ChangeStorageMaterielDetailActivity;
import com.inossem.warehousestorage_inossem.activity.change_storage.ChangeStorageMaterielListActivity;
import com.inossem.warehousestorage_inossem.activity.change_storage.extra.ChangeStorageExtra;
import com.inossem.warehousestorage_inossem.bean.changeStorage.ChangeStorageMaterielListBean;
import com.inossem.warehousestorage_inossem.utils.QRCodeEditTextWatcher;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

/**
 * Created by leij on 2017/6/6.
 */

public class ChangeStorageListAdapter extends BaseAdapter {

    private static final String SPECIAL_INVENTORY_CODE = "特殊库存编码:";

    private static final String POSITION = "仓位:";

    private static final String BATCH = "批次:";

    private Context mContext;
    private List<ChangeStorageMaterielListBean.MaterielListBean> mChangeStorageMaterielList;
    private LayoutInflater inflater;
    private ChangeStorageMaterielListActivity.ChangeStorageMaterielListener mChangeStorageMaterielListener;
    private String mCondition;

    public ChangeStorageListAdapter(Context context, List<ChangeStorageMaterielListBean.MaterielListBean> changeStorageMaterielList, String condition, ChangeStorageMaterielListActivity.ChangeStorageMaterielListener changeStorageMaterielListener) {
        mContext = context;
        mChangeStorageMaterielList = changeStorageMaterielList;
        mChangeStorageMaterielListener = changeStorageMaterielListener;
        inflater = LayoutInflater.from(context);
        mCondition = condition;
    }

    @Override
    public int getCount() {
        return mChangeStorageMaterielList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mChangeStorageMaterielList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_in_storage_receive_list_first_item, null);
            EditText condition = (EditText) convertView.findViewById(R.id.inStorageNumber);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            condition.setHint(R.string.activity_change_storage_materiel_list_please_input_material_information);
            condition.setText(mCondition);
            condition.addTextChangedListener(new QRCodeEditTextWatcher(condition) {
                @Override
                public void onScan(String text) {
                    Utils.closeSoftKeyboard(mContext, condition);
                    mChangeStorageMaterielListener.onNoDoubleClickListener(text);
                }
            });
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    mChangeStorageMaterielListener.onNoDoubleClickListener(condition.getText().toString());
                }
            });
        } else {
            ChangeStorageMaterielListBean.MaterielListBean materielItem = mChangeStorageMaterielList.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_change_storage_materiel_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, materielItem);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.materielCode = (TextView) view.findViewById(R.id.materielCode);
        viewHolder.specialInventoryCode = (TextView) view.findViewById(R.id.specialInventoryCode);
        viewHolder.materielDescribe = (TextView) view.findViewById(R.id.materielDescribe);
        viewHolder.position = (TextView) view.findViewById(R.id.position);
        viewHolder.batch = (TextView) view.findViewById(R.id.batch);
        viewHolder.number = (TextView) view.findViewById(R.id.number);
        viewHolder.headBar = (PercentRelativeLayout) view.findViewById(R.id.headBar);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, ChangeStorageMaterielListBean.MaterielListBean materielItem) {
        viewHolder.materielCode.setText(materielItem.getMat_code());
        if (TextUtils.isEmpty(materielItem.getSpec_stock_code())) {
            viewHolder.specialInventoryCode.setVisibility(View.GONE);
        } else {
            viewHolder.specialInventoryCode.setVisibility(View.VISIBLE);
            viewHolder.specialInventoryCode.setText(getBuilder(context, SPECIAL_INVENTORY_CODE, materielItem.getSpec_stock_code()));
        }
        viewHolder.position.setText(getBuilder(context, POSITION, materielItem.getPosition_code()));
        viewHolder.batch.setText(getBuilder(context, BATCH, materielItem.getBatch()));
        viewHolder.materielDescribe.setText(materielItem.getMat_name());
        if (TextUtils.isEmpty(materielItem.getNumber())) {
            viewHolder.number.setText(R.string.activity_change_storage_materiel_detail_enter_the_number_of);
            viewHolder.headBar.setBackgroundResource(R.color.view_receive_item_list_dark_row);
        } else {
            viewHolder.number.setText(materielItem.getNumber() + materielItem.getUnit_name());
            viewHolder.headBar.setBackgroundResource(R.color.base_green);
        }
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(context, ChangeStorageMaterielDetailActivity.class);
//                intent.putExtra(ChangeStorageExtra.MAT_ID, materielItem.getMat_id());
//                intent.putExtra(ChangeStorageExtra.LOCATION_ID, materielItem.getLocation_id());
                intent.putExtra(ChangeStorageExtra.STOCK_POSITION_ID, materielItem.getStock_position_id());
//                intent.putExtra(ChangeStorageExtra.BATCH, materielItem.getBatch());
//                LogUtils.i("mMatId == " + materielItem.getMat_id() + "     mLocationId === " + materielItem.getLocation_id() + "    mStockPositionId === " + materielItem.getStock_position_id()
//                        + "   mBatch === " + materielItem.getBatch());
                context.startActivity(intent);
            }
        });

        view.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                mChangeStorageMaterielListener.deleteMaterielItem(materielItem.getMat_id() ,materielItem.getLocation_id() , materielItem.getStock_position_id() , materielItem.getBatch(), materielItem.getMat_code());
                return true;
            }
        });

    }

    private SpannableStringBuilder getBuilder(Context context, String changeColorMessage, String message) {
        SpannableStringBuilder builder = new SpannableStringBuilder(changeColorMessage + message);
        ForegroundColorSpan redSpan = new ForegroundColorSpan(context.getResources().getColor(R.color.common_font_item_name));
        builder.setSpan(redSpan, 0, changeColorMessage.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return builder;
    }

    private class ViewHolder {
        private TextView materielCode, specialInventoryCode, materielDescribe, position, batch, number;
        private PercentRelativeLayout headBar;
    }
}
